package org.bw.bookstore.book.service.impl;




import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.bw.bookstore.book.mapper.StaffMemberMapper;
import org.bw.bookstore.book.service.StaffMemberService;
import org.bw.bookstore.common.entity.TbStaffMember;
import org.bw.bookstore.common.utils.ConstantSet;
import org.bw.bookstore.common.utils.JwtTokenUtils;
import org.bw.bookstore.common.utils.ResponseResult;
import org.bw.bookstore.common.utils.StringTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @program:wisdom_bookstore
 * @description: 管理人员实现类
 * @author:liuwu
 * @create:2020-10-22 14:00
 **/
@Service
@Slf4j
public class StaffMemberServiceImpl implements StaffMemberService {

    @Autowired
    private StaffMemberMapper staffMemberMapper;


    @Override
    public ResponseResult addStaffMember(TbStaffMember staffMember) {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();
        //检查工作人员是否存在
        TbStaffMember tbStaffMember= staffMemberMapper.selectOne(new QueryWrapper<TbStaffMember>()
                .eq("user_name",staffMember.getUserName())
                .last("limit 1"));

         //工作人员已存在
        if (tbStaffMember!=null){
            result.setSuccess(false);
            result.setCode(-1);
            result.setMessage("工作人员已存在");
            return result;
        }

        tbStaffMember = new TbStaffMember();

        //随机生成code
        tbStaffMember.setCode(StringTools.getRandomString(10));

        //生成盐
        String salt = StringTools.getRandomString(32);
        tbStaffMember.setUserSale(salt);
        //密码+.+盐
        String password=staffMember.getUserPass()+"."+salt;
        //MD5加密
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        log.info("pass:"+md5Password);
        tbStaffMember.setUserPass(md5Password);

        tbStaffMember.setStaffMemberPhone(staffMember.getStaffMemberPhone());
        tbStaffMember.setSpecificPosition(staffMember.getSpecificPosition());
        tbStaffMember.setRealName(staffMember.getRealName());
        tbStaffMember.setUserName(staffMember.getUserName());
        tbStaffMember.setHireDate(new Date());
        tbStaffMember.setCreateTime(new Date());
        tbStaffMember.setShopCode(staffMember.getShopCode());
        //默认为在职
        tbStaffMember.setDeleted(ConstantSet.NOT);
        //添加
        staffMemberMapper.insert(tbStaffMember);
        if (tbStaffMember.getCode()!=null){
            tbStaffMember.setUserPass("");
            tbStaffMember.setUserSale("");
            resultMap.put("tbStaffMember",tbStaffMember);
            return result;
        }

        result.setSuccess(false);
        result.setCode(-1);
        result.setMessage("工作人员添加失败");
        return result;

    }

    @Override
    public ResponseResult delStaffMember(Integer id) {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();

        //--1 判断工作人员是否存在
        TbStaffMember tbStaffMember= staffMemberMapper.selectById(id);
        //工作人员不存在
        if(tbStaffMember == null) {
            result.setSuccess(false);
            result.setCode(-1);
            result.setMessage("工作人员不存在");
            return result;
        }

        //工作人员已删除
        if(!tbStaffMember.getDeleted().equals(ConstantSet.NOT)) {
            result.setSuccess(false);
            result.setCode(-1);
            result.setMessage("工作人员已删除");
            return result;
        }

        //--2 设置删除标志
        tbStaffMember.setDeleted(1);
        //逻辑删除
        staffMemberMapper.updateById(tbStaffMember);

        resultMap.put("tbStaffMember",tbStaffMember);
        result.setResult(resultMap);

        return result;
    }

    @Override
    public ResponseResult updateStaffMember(TbStaffMember staffMember) {
        ResponseResult result=ResponseResult.SUCCESS();
        TbStaffMember tbStaffMember = staffMemberMapper.selectById(staffMember.getId());
        //工作人员不存在
        if(tbStaffMember == null) {
            result.setSuccess(false);
            result.setMessage("工作人员不存在");
            return result;
        }
        System.err.println("____"+staffMember);
        tbStaffMember.setShopCode(staffMember.getShopCode());
        tbStaffMember.setStaffMemberPhone(staffMember.getStaffMemberPhone());
        tbStaffMember.setSpecificPosition(staffMember.getSpecificPosition());
        tbStaffMember.setUserName(staffMember.getUserName());
        tbStaffMember.setHireDate(staffMember.getHireDate());
        tbStaffMember.setUpdateTime(new Date());
        tbStaffMember.setRealName(staffMember.getRealName());
        //进行修改
        staffMemberMapper.updateById(tbStaffMember);
        return result;
    }

    @Override
    public ResponseResult loginStaffMember(String userName, String userPass) {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();
        //查询工作人员
        TbStaffMember tbStaffMember= staffMemberMapper.selectOne(new QueryWrapper<TbStaffMember>()
                .eq("user_name",userName)
                .eq("deleted", ConstantSet.NOT)
                .last("limit 1"));
        //工作人员不存在
        if(tbStaffMember == null) {
            result.setSuccess(false);
            result.setMessage("工作人员不存在");
            return result;
        }

        //判断密码
        String password = userPass + "." +tbStaffMember.getUserSale();
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!tbStaffMember.getUserPass().equals(md5Password)){
            result.setSuccess(false);
            result.setMessage("工作人员密码不正确");
            return result;
        }

        //生成token
        String token = JwtTokenUtils.makeToken(tbStaffMember.getId(), tbStaffMember.getUserName(), "tbStaffMember");
        resultMap.put("tbStaffMember", tbStaffMember);
        resultMap.put("token", token);
        result.setResult(resultMap);
        return result;
    }

    @Override
    public ResponseResult getStaffMember() {

        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();
        List<TbStaffMember> staffMembers=staffMemberMapper.selectList(
                new QueryWrapper<TbStaffMember>()
                .select("id,code,create_time,real_name,user_name,specific_position,staff_member_Phone,hire_date,deleted")
                .orderByDesc("id"));
        resultMap.put("staffMembers",staffMembers);
        result.setResult(resultMap);
        return result;
    }

    @Override
    public ResponseResult getStaffMemberBySomething(TbStaffMember staffMember,Integer currentPage,Integer pageSize) {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();
        //条件构造器
        QueryWrapper queryWrapper = new QueryWrapper<TbStaffMember>();
        //模糊查询真实姓名
        if (staffMember.getRealName()!=null && !"".equals(staffMember.getRealName())){
            queryWrapper.like("real_name","%"+staffMember.getRealName()+"%");
        }
        //模糊查询手机号
        if (staffMember.getStaffMemberPhone()!=null && !"".equals(staffMember.getStaffMemberPhone())){
            queryWrapper.like("staff_member_Phone","%"+staffMember.getStaffMemberPhone()+"%");
        }
        //条件查询是职位
        if (staffMember.getSpecificPosition()!=null ){
            queryWrapper.eq("specific_position",staffMember.getSpecificPosition());
        }
        //分页
        Page<TbStaffMember> page = new Page<>(currentPage,pageSize);
        IPage<TbStaffMember> iPage = staffMemberMapper.selectPage(page, queryWrapper);
        resultMap.put("iPage",iPage);

        result.setResult(resultMap);
        return result;
    }

    @Override
    public ResponseResult getStaffMemberById(Integer id) {
        ResponseResult result = ResponseResult.SUCCESS();
        //创建map集合，用于存储对象
        HashMap<String, Object> resultMap = new HashMap<>();
        //条件查询 返回实体对象
        TbStaffMember staffMember = staffMemberMapper.selectOne(new QueryWrapper<TbStaffMember>()
                .select("id,code,create_time,real_name,user_name,specific_position,staff_member_Phone,hire_date,deleted")
                .eq("id", id)
                .last("limit 1"));
        //判断是否存在
        if (staffMember==null){
            result.setSuccess(false);
            result.setMessage("工作人员不存在");
            return result;
        }

        //存入map集合
        resultMap.put("staffMember",staffMember);
        result.setResult(resultMap);
        return  result;
    }
}
